package modular {
	import ch.sfug.log.Logger;
	
	import modular.module.Module;	

	/**
	 * @author mich
	 */
	public class Modular {
		
		private static var _rootLoader:ModuleSetLoader;
		public static var LOG_INFO:String = "modular_log_info";
		public static var LOG_ERROR:String = "modular_log_error";

		
		public static function getModuleByID( id:String ):Module {
			var mod:Module = _rootLoader.getModuleByID( id );
			if( mod != null ) {
				return mod;
			} else {
				Logger.log( "cant find module with id: " + id, LOG_ERROR );
				throw new Error( "cant find module with id: " + id );
			}
		}
		
		public static function getModuleByClass( className:String ):Module {
			var mod:Module = _rootLoader.getModuleByClass( className );
			if( mod != null ) {
				return mod;
			} else {
				Logger.log( "cant find module with classname: " + className, LOG_ERROR );
				throw new Error( "cant find module with classname: " + className );
			}
		}
		
		/**
		 * tries to find a module with the id as a xml property. if not found it tries to find the module by className.
		 */
		public static function getModule( id:String ):Module {
			var mod:Module = _rootLoader.getModuleByID( id );
			if( mod == null ) mod = _rootLoader.getModuleByClass( id );
			if( mod != null ) {
				return mod;
			} else {
				Logger.log( "cant find module with id or classname like: " + id, LOG_ERROR );
				throw new Error( "cant find module with id or classname like: " + id );
			}
		}

		
		
		public static function loadInto( ms:ModuleSet, id:String ):void {
			var msl:ModuleSetLoader = ModuleSetLoader( _rootLoader.getModuleByID( id ) );
			if( msl != null ) {
				msl.loadModuleSet( ms );
			} else {
				Logger.log( "cant find ModuleSetLoader with id: " + id, LOG_ERROR );
			}
		}
		
		internal static function setRootLoader( rl:ModuleSetLoader ):void {
			_rootLoader = rl;
		}
		
		public static function rootLoader( ):ModuleSetLoader {
			return _rootLoader;
		}
		
	}
}
